package com.novellotus.iov.carvice.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.novellotus.iov.carvice.R;

/**
 * Created by Rich on 2017/7/19.
 */

public class MnScaleBar extends View {
    private Context mContext;


    private int max = 100; //最大刻度

    private float scaleMargin = 1; //刻度间距
    private int type = 1;
    private float scaleDistance = 1;
    private String percentValue;
    private int percent = 0;
    //  Paint mPaint;

    private String tag = MnScaleBar.class.getSimpleName();

    public MnScaleBar(Context context) {
        this(context, null);
        Log.i("igd", "1");
    }

    public MnScaleBar(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
        Log.i("igd", "2");
    }

    public MnScaleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        Log.i("igd", "3");
    }

    private float shortScale;
    private float longScale;

    @Override
    protected void onDraw(Canvas canvas) {

        shortScale = getResources().getDimension(R.dimen.short_scale);//短刻度长度
        longScale = getResources().getDimension(R.dimen.long_scale);//长刻度长度
        scaleDistance = getResources().getDimension(R.dimen.margin_scale);//刻度尺尺边界的误差间距
        onDrawScale(canvas); //画刻度
        super.onDraw(canvas);
        if (percent != 0) {
            setProgress(canvas);
        }
    }


    /**
     * 画进度条背景
     * 1是充电刻度  2是发动机刻度
     */
    public void setPercent(int percent, int type) {

        this.type = type;
        if (type == 1) {
            this.percent = percent;
            percentValue = percent + "%";
        } else {
            this.percent = percent * 100 / 180;
            if (percent > 100) {
                this.percent = 100;
            }
            percentValue = percent + "";
        }

        invalidate();
    }


    /**
     * 画进度条
     */
    private void setProgress(Canvas canvas) {
        float progressPercent = getWidth() * percent * 0.01f;

        //画深色背景
//        Rect rect = new Rect(0, (int) getResources().getDimension(R.dimen.rect_padding_top), getWidth(), getHeight() - (int) longScale);
//        Paint paintBg = new Paint();
//        paintBg.setStyle(Paint.Style.FILL);
//        paintBg.setColor(Color.parseColor("#1c2050"));
//        paintBg.setAntiAlias(true);
//        canvas.drawRect(rect, paintBg);

        //画不规则矩形
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        LinearGradient gradient;
        if (type == 1) {
            //充电
            gradient = new LinearGradient(0, 0, progressPercent, getHeight() - longScale, Color.parseColor("#1f5383"), Color.parseColor("#00f2d9"), Shader.TileMode.MIRROR);
        } else {
            //速度
            gradient = new LinearGradient(0, 0, progressPercent, getHeight() - longScale, Color.parseColor("#163764"), Color.parseColor("#15bdfd"), Shader.TileMode.MIRROR);
        }
        paint.setShader(gradient);
        Path path = new Path();
        // 此点为多边形的起点 左上角
        path.moveTo(0, 0);
        //上横线
        if (percent == 100) {
            path.lineTo(progressPercent, 0);
        } else {
            path.lineTo(progressPercent - getResources().getDimension(R.dimen.rect_long), 0);
        }

        //竖线或者横线
        path.lineTo(progressPercent, getHeight() - longScale);
        //底线
        path.lineTo(0, getHeight() - longScale);


        path.close(); // 使这些点构成封闭的多边形
        canvas.drawPath(path, paint);
        // canvas.drawRect(rect, paint);
        //画百分比数字
        Paint textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(getResources().getDimension(R.dimen.scale_text));
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();

        float top = fontMetrics.top;//为基线到字体上边框的距离,即上图中的top
        float bottom = fontMetrics.bottom;//为基线到字体下边框的距离,即上图中的bottom
        if (percent <= 10) {
            textPaint.setTextAlign(Paint.Align.LEFT);
            canvas.drawText(percentValue, getResources().getDimension(R.dimen.long_scale), (getHeight() - longScale) / 2 - top / 2 - bottom / 2, textPaint);
        } else if (percent == 100) {
            textPaint.setTextAlign(Paint.Align.RIGHT);
            canvas.drawText(percentValue, progressPercent, (getHeight() - longScale) / 2 - top / 2 - bottom / 2, textPaint);
        } else {
            textPaint.setTextAlign(Paint.Align.RIGHT);
            canvas.drawText(percentValue, progressPercent - getResources().getDimension(R.dimen.rect_long), (getHeight() - longScale) / 2 - top / 2 - bottom / 2, textPaint);
        }

    }


    /**
     * 画刻度
     */
    private void onDrawScale(Canvas canvas) {
        if (canvas == null || getWidth() == 0) return;
        scaleMargin = getWidth() / 100f;
        Paint mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#ffffff"));
        mPaint.setTextAlign(Paint.Align.CENTER); //文字居中
        mPaint.setTextSize(20);
        for (int i = 0; i <= max; i++) {
            if (i % 10 == 0) {
                if (i == max) {
                    canvas.drawLine(i * scaleMargin - scaleDistance * 1.1f, getHeight() - longScale, i * scaleMargin - scaleDistance * 1.1f, getHeight() - longScale + longScale, mPaint);
                } else if (i == 0) {
                    canvas.drawLine(i * scaleMargin + scaleDistance * 1.1f, getHeight() - longScale, i * scaleMargin + scaleDistance * 1.1f, getHeight() - longScale + longScale, mPaint);
                } else {
                    canvas.drawLine(i * scaleMargin, getHeight() - longScale, i * scaleMargin, getHeight() - longScale + longScale, mPaint);
                }
            } else {
                canvas.drawLine(i * scaleMargin, getHeight() - longScale, i * scaleMargin, getHeight() - longScale + shortScale, mPaint);
            }
        }
    }


}
